package com.gdsdxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdsdxy.dto.LabelFromDTO;
import com.gdsdxy.dto.ResultVo;
import com.gdsdxy.entity.BlogLabel;
import com.gdsdxy.mapper.BlogLabelMapper;
import com.gdsdxy.service.IBlogLabelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BlogLabelServiceImpl extends ServiceImpl<BlogLabelMapper, BlogLabel> implements IBlogLabelService {

    @Resource
    private BlogLabelMapper blogLabelMapper;

    @Override
    public ResultVo addLabel(LabelFromDTO labelFromDTO) {
        // 1.查询标签
        Integer count = query().eq("name", labelFromDTO.getName()).count();

        // 2.判断标签是否存在
        if (count > 0) {
            return ResultVo.fail("标签已经存在");
        }

        // 添加标签
        BlogLabel blogLabel = BeanUtil.copyProperties(labelFromDTO, BlogLabel.class);
        boolean result = save(blogLabel);
        return result ? ResultVo.ok("添加成功") : ResultVo.fail("添加失败");
    }

    @Override
    public ResultVo selectLabel(Long id) {
        BlogLabel label = getById(id);
        return label != null ? ResultVo.ok(label) : ResultVo.fail("标签不存在");
    }

    @Override
    public ResultVo selectLabelList(List<Long> ids) {
        List<BlogLabel> blogLabels = blogLabelMapper.selectBatchIds(ids);
        return blogLabels != null ? ResultVo.ok(blogLabels) : ResultVo.fail("标签不存在");
    }

}
